package com.cheng.controller;

import com.cheng.pojo.*;
import com.cheng.service.appcategory.AppCategoryService;
import com.cheng.service.appinfo.AppInfoService;
import com.cheng.service.appversion.AppVersionService;
import com.cheng.service.datadictionary.DataDictionaryService;
import com.cheng.utils.PageUtils;
import com.cheng.utils.UUIDutils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@SuppressWarnings("all")
@RequestMapping(value = "/devApp")
public class DevAppInfoController {
    @Autowired
    private AppCategoryService appCategoryService;
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @Autowired
    private AppInfoService appInfoService;
    @Autowired
    private AppVersionService appVersionService;

    @RequestMapping(value = "/queryAppinfo")
    public String queryAppinfo(HttpServletRequest request, Model model) {
        String querySoftwareName = request.getParameter("querySoftwareName");
        String queryStatus = request.getParameter("queryStatus");
        String queryFlatformId = request.getParameter("queryFlatformId");
        String pageIndex = request.getParameter("pageIndex");

        String queryCategoryLevel1 = request.getParameter("queryCategoryLevel1");
        String queryCategoryLevel2 = request.getParameter("queryCategoryLevel2");
        String queryCategoryLevel3 = request.getParameter("queryCategoryLevel3");

        AppInfo appInfo = new AppInfo();
        if (pageIndex == null) {
            pageIndex = "1";
        }

        if (querySoftwareName == null) {
            appInfo.setSoftwareName("");
        } else {
            appInfo.setSoftwareName(querySoftwareName);
        }

        if (queryStatus != null && queryStatus != "") {
            appInfo.setStatus(Integer.parseInt(queryStatus));
        }

        if (queryFlatformId != null && queryFlatformId != "") {
            appInfo.setFlatformId(Integer.parseInt(queryFlatformId));
        }

        if (queryCategoryLevel1 != null && queryCategoryLevel1 != "") {
            appInfo.setCategoryLevel1(Integer.parseInt(queryCategoryLevel1));
        }

        if (queryCategoryLevel2 != null && queryCategoryLevel2 != "") {
            appInfo.setCategoryLevel2(Integer.parseInt(queryCategoryLevel2));
        }

        if (queryCategoryLevel3 != null && queryCategoryLevel3 != "") {
            appInfo.setCategoryLevel3(Integer.parseInt(queryCategoryLevel3));
        }

        PageUtils<AppInfo> pageUtils = new PageUtils<>();
        pageUtils.setCurrentPage(Integer.parseInt(pageIndex));
        pageUtils.setPageSize(5);

        appInfoService.findAll(appInfo, pageUtils);

        model.addAttribute("page", pageUtils);

        List<DataDictionary> statusList = dataDictionaryService.getAllAppStatus();
        model.addAttribute("statusList", statusList);

        List<DataDictionary> flatFormList = dataDictionaryService.getAllFlatform();
        model.addAttribute("flatFormList", flatFormList);

        List<AppCategory> categoryLevel1List = appCategoryService.getTypeLevel1();
        model.addAttribute("categoryLevel1List", categoryLevel1List);
        return "developer/appinfolist";
    }

    @RequestMapping(value = "/addPage", method = RequestMethod.GET)
    public String addPage() {
        return "developer/appinfoadd";
    }

    @RequestMapping(value = "/apkExist")
    @ResponseBody
    public AppInfo apkExist(@RequestParam(value = "APKName", required = false) String APKName) {
        AppInfo appInfo = new AppInfo();
        if (APKName == null || APKName.trim() == "") {
            appInfo.setApkName("empty");
        } else {
            int count = appInfoService.isAppInfo(APKName);
            if (count > 0) {
                appInfo.setApkName("exist");
            } else {
                appInfo.setApkName("noexist");
            }
        }
        return appInfo;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(AppInfo appInfo, Model model, MultipartFile a_logoPicPath, HttpSession session) {
        String realPath = "E:\\code\\JavaIDEA\\练习\\AppManagement\\src\\main\\webapp\\statics\\uploadfiles";
        if (!a_logoPicPath.isEmpty()) {
            String filename = a_logoPicPath.getOriginalFilename();
            String extension = FilenameUtils.getExtension(filename);
            if (extension.equalsIgnoreCase("ico")
                    || extension.equalsIgnoreCase("jpeg")
                    || extension.equalsIgnoreCase("jpe")
                    || extension.equalsIgnoreCase("png")
                    || extension.equalsIgnoreCase("gif")
                    || extension.equalsIgnoreCase("bmp")
            ) {
                filename = UUIDutils.getUUID() + "." + extension;
                try {
                    a_logoPicPath.transferTo(new File(realPath, filename));

                    DevUser user = (DevUser) session.getAttribute("devUserSession");
                    appInfo.setDevId(user.getId());
                    appInfo.setCreatedBy(user.getId());

                    appInfo.setLogoPicPath("statics/uploadfiles/" + filename);
                    appInfo.setLogoLocPath(realPath + "/" + filename);
                    int count = appInfoService.addAppInfo(appInfo);
                    if (count > 0) {
                        return "redirect:/devApp/queryAppinfo";
                    } else {
                        model.addAttribute("msg", "修改失败");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                model.addAttribute("msg", "图片类型不正确");
            }
        } else {
            model.addAttribute("msg", "没有选择图片");
        }
        return "developer/appinfoadd";
    }

    @RequestMapping(value = "/view/{id}")
    public String viewPage(@PathVariable Integer id, Model model) {
        AppInfo appInfo = new AppInfo();
        appInfo.setId(id);
        appInfo = appInfoService.getAppInfoById(appInfo);
        model.addAttribute("appInfo", appInfo);

        List<AppVersion> list = appVersionService.getHistoryVersionById(id);
        model.addAttribute("appVersionList", list);

        return "developer/appinfoview";
    }
}
